package com.xin.di.uav.applet.controller;

import com.xin.di.uav.applet.mapper.NewUserMapper;
import com.xin.di.uav.applet.request.ServiceAreaAddParam;
import com.xin.di.uav.applet.request.ServiceAreaEditParam;
import com.xin.di.uav.applet.resp.UserResp;
import com.xin.di.uav.applet.service.NewUserService;
import com.xin.di.uav.common.request.common.IdRequest;
import com.xin.di.uav.common.utils.Rs;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/home/user/")
public class UserController {
    @Autowired
    private NewUserService newUserService;

    @Autowired
    private NewUserMapper newUserMapper;

    @PostMapping("add")
    public Rs<Boolean> add(@RequestBody @Valid ServiceAreaAddParam serviceArea) {
        return Rs.success(newUserService.add(serviceArea));
    }

//    @DeleteMapping("{id}")
//    public Rs<Boolean> delete(@PathVariable String id) {
//        return Rs.success(serviceAreaService.delete(id));
//    }

    @PutMapping("update")
    public Rs<Boolean> update(@RequestBody @Valid ServiceAreaEditParam serviceArea) {
        return Rs.success(newUserService.update(serviceArea));
    }

    @PutMapping("update-status")
    public Rs<Boolean> updateStatus(@RequestBody @Valid IdRequest idRequest) {
        return Rs.success(newUserService.updateStatus(idRequest));
    }

    @GetMapping("list")
    public Rs<UserResp> list(@RequestParam int page, @RequestParam int pageSize, @RequestParam(required = false) String phone, @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime, @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        UserResp userResp = new UserResp();
        int cnt = newUserMapper.count(phone, startTime, endTime);
        userResp.setTotal(cnt);
        if (cnt > 0) {
            List<UserResp.Record> records = newUserService.list(page, pageSize, phone, startTime, endTime);
            userResp.setRecordList(records);
        }
        return Rs.success(userResp);
    }


    @Operation(summary = "列表导出")
    @GetMapping("export")
    public void export(HttpServletResponse httpServletResponse, @RequestParam(required = false) String phone, @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime, @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        newUserService.export(httpServletResponse, phone, startTime, endTime);
    }

}
